
package controllers;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.ButtonRequest;
import model.ButtonRequestModel;
import model.ComplaintModel;
import view.FeedbackHistoryDetailsGUI;
import view.FeedbackHistoryGUI;
import client.CliMessage;


	/**
	 * This Class is a Feedback History Controller which extends AbstractController
	 * @author Tal 
	 *
	 */

	public class FeedbackHistoryController extends AbstractController
	{	
				private FeedbackHistoryGUI FeedbackHistoryView;
			    private ButtonRequestModel reqModel;
	
				
				/**
				 * This is Feedback History  constructor.
				 * @param FeedbackHistoryView is a Feedback History window GUI instance.
				 * @param reqModel is Button Request Model instance.
				 */
				public FeedbackHistoryController(FeedbackHistoryGUI FeedbackHistoryView,ButtonRequestModel rModel) 
				{
					super(FeedbackHistoryView,rModel);
					this.FeedbackHistoryView = FeedbackHistoryView;
					this.reqModel=rModel;
				
				}
				
				/**
				 * This is setFeedbackHistoryButtons method-this method add listeners to buttons in Feedback History window.
				 * 
				 */
				
				private void setFeedbackHistoryButtons(ArrayList<ButtonRequest>  btnReq)
				{
					
					int nButtons = btnReq.size();
					
					
					//set a HashMap that contains all possible button listeners by their names
					ArrayList<ButtonListener> al = new ArrayList<ButtonListener>();
					ArrayList<ActionListener>  btnLis=new ArrayList<ActionListener>();
					
					//set  specific complaints buttons
					for(int i = 0; i < nButtons ; i++)
					{
						al.add(new ButtonListener(this,btnReq.get(i).getButtonReqID(),btnReq.get(i).getButtonUID()));
						btnLis.add(al.get(i));
						FeedbackHistoryView.addButtonListener(btnLis.get(i),i);
					}	
				
				}
				
				public String getStatusByName(int sts)
				{
					String statusName=null;
					  switch (sts) {
			            case 0:  statusName = "Unhandeled";
			                     break;
			            case 1:  statusName = "In Process";
			                     break;
			            case 2:  statusName = "Handeled";
			                     break;
			            default: statusName = "Invalid";
			                     break;
			        }
					  
					  return statusName;
					  
				}
				
				/**
				 * Inner class that handles when Button Pressed by request Id, extends AbstractListener
				 * @author Tal
				 *
				 */
				
				class ButtonListener extends AbstractListener
				{
					private FeedbackHistoryController previusObserver;
					private String ComID;
					private String Uid;
					
					public ButtonListener(FeedbackHistoryController previusObserver, String comid,String uid)
					{
						this.ComID=comid;
						this.Uid=uid;
						this.previusObserver=previusObserver;
					}
					
					@Override
					public void actionPerformed(ActionEvent arg0) 
					{
						
						
						//create a new Feedback History Details controller
						FeedbackHistoryDetailsGUI feedHisDeatilsView = new FeedbackHistoryDetailsGUI();
						ComplaintModel ComplaintModel = new ComplaintModel(Integer.parseInt(Uid),Integer.parseInt(ComID));
						
						FeedbackHistoryDetailsController feedHisDetController = new FeedbackHistoryDetailsController(feedHisDeatilsView,ComplaintModel);
										
						//add observer to new controller and delete the rest
						setCurrentObserver(feedHisDetController);						
						
						//add a listener to "back" button in Feedback History Details window
						setBackListener(previusObserver,feedHisDeatilsView);
						
						String searchQuery = "search"	+"SELECT * "
								+" FROM customer_complaints AS c"
                                +" WHERE c.comid = " + ComID + " ";
                               

                          sendToServer(searchQuery);
						
									
					}
				}

				@Override
				public void handleDBRequestSuccess()
				{
					FeedbackHistoryView.displayWrnMessage("Feedback History Found:");
					
				}
				
				@Override
				public void handleDBSearchResult(CliMessage dbResult) 
				{	
					//get a list of all feedback history
					ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
					ArrayList<ButtonRequest>  btnReq= reqModel.getButtonRequest();
					int rows=srs.size();
					int column=3;
					
					//create data table
					 Object[][] data=new Object[rows][column];
				   	
					 String[] columnNames={"Feedback Id","Date","Status"};

					
					for(int i = 0 ; i <rows  ; i++)
					{
						Map<String, String> feedHisList  = srs.get(i);	
						
						  //add uid & comId button to arraylist
						btnReq.add(new ButtonRequest(feedHisList.get("COMID"),feedHisList.get("UID")));
								
						
						//comId
						 data[i][0]=feedHisList.get("COMID");		
						 //date
						 data[i][1]=feedHisList.get("DATE");
						 
						 int status=Integer.valueOf(feedHisList.get("STATUS")); 
						//Status
						data[i][2]=getStatusByName(status);
						 
						
						 System.out.println("com id: "+feedHisList.get("COMID")+"Date: "+ feedHisList.get("DATE") + "Status: "+feedHisList.get("STATUS"));
							 					
					}	
					
					//set new data table
					FeedbackHistoryView.setTable(columnNames, data);
					
					//create n buttons
					int numberOfRequests = btnReq.size();
					
					FeedbackHistoryView.setFeedBackHistorybuttons(numberOfRequests);
					
					//Set list button  listener	
					setFeedbackHistoryButtons(btnReq);
					
					//display main window
					FeedbackHistoryView.setVisible(true);
					FeedbackHistoryView.setTableVisibilty(true);	
							
				}

				@Override
				public void handleNullSearchResult() 
				{
					//display main window
					FeedbackHistoryView.setVisible(true);
					FeedbackHistoryView.disposeTable();
					FeedbackHistoryView.disposeExistingButtons();
					FeedbackHistoryView.displayWrnMessage("Feedback History Not Found!!!");
					
				}


}
